# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/3/28 17:11
"""
from threading import Thread

import request.open.robot as robot
from config import data as config
from config.core.keys import ROBOT_NUMBER
from service import run, shutdown
from common.logger.log import log_


if __name__ == '__main__':
    # 以多线程启动 flask 服务
    thread = Thread(target=run, args=('0.0.0.0', config.get('flask.port'),))
    thread.start()
    # 注册机器
    result = robot.reg(config.get(ROBOT_NUMBER), config.get('flask.port'), config.get('robot.remote_code'),
                       config.get('robot.remote_pwd'))
    # 注册机器成功设置本机机器吗
    if result['code'] == 0:
        log_.info("注册机器到服务器成功")
        config.set(ROBOT_NUMBER, result['result'])
        config.save()
    else:
        log_.error("注册机器到服务器失败")
        # 失败暂停服务
        shutdown()
    thread.join()
